package com.todod.base.module.base.controller.admin.rtmaintain;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.framework.common.util.object.BeanUtils;
import static com.todod.base.framework.common.pojo.CommonResult.success;

import com.todod.base.framework.excel.core.util.ExcelUtils;

import com.todod.base.framework.apilog.core.annotation.ApiAccessLog;
import static com.todod.base.framework.apilog.core.enums.OperateTypeEnum.*;

import com.todod.base.module.base.controller.admin.rtmaintain.vo.*;
import com.todod.base.module.base.dal.dataobject.rtmaintain.RtMaintainDO;
import com.todod.base.module.base.service.rtmaintain.RtMaintainService;

@Tag(name = "管理后台 - 实时数据维护")
@RestController
@RequestMapping("/base/rt-maintain")
@Validated
public class RtMaintainController {

    @Resource
    private RtMaintainService rtMaintainService;

    @PostMapping("/create")
    @Operation(summary = "创建实时数据维护")
    public CommonResult<Long> createRtMaintain(@Valid @RequestBody RtMaintainSaveReqVO createReqVO) {
        return success(rtMaintainService.createRtMaintain(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新实时数据维护")
    public CommonResult<Boolean> updateRtMaintain(@Valid @RequestBody RtMaintainSaveReqVO updateReqVO) {
        rtMaintainService.updateRtMaintain(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除实时数据维护")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteRtMaintain(@RequestParam("id") Long id) {
        rtMaintainService.deleteRtMaintain(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得实时数据维护")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<RtMaintainRespVO> getRtMaintain(@RequestParam("id") Long id) {
        RtMaintainDO rtMaintain = rtMaintainService.getRtMaintain(id);
        return success(BeanUtils.toBean(rtMaintain, RtMaintainRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得实时数据维护分页")
    public CommonResult<PageResult<RtMaintainRespVO>> getRtMaintainPage(@Valid RtMaintainPageReqVO pageReqVO) {
        PageResult<RtMaintainDO> pageResult = rtMaintainService.getRtMaintainPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RtMaintainRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出实时数据维护 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRtMaintainExcel(@Valid RtMaintainPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RtMaintainDO> list = rtMaintainService.getRtMaintainPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "实时数据维护.xls", "数据", RtMaintainRespVO.class,
                        BeanUtils.toBean(list, RtMaintainRespVO.class));
    }

}